package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.atguigu.gmall.product.model.*;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * title:
 * author: Gyh
 * date:2023-04-22 9:16
 * Description:
 */
@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private Executor executor;

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 详情接口
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getItemInfo(Long skuId) {
        HashMap<String, Object> ResultMap = new HashMap<>();
        //判断用户访问的商品是否存在，如果没有则直接返回，有就远程调用
        //todo 开发阶段为了测试,暂时注释
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
//        boolean contains = bloomFilter.contains(skuId);
//        if (!contains){
//            throw new RuntimeException("访问的数据不存在");
//        }

        //创建supplyAsync有返回值异步操作对象
        //远程调用商品服务-根据skuID查询商品sku信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            if (skuInfo != null) {
                ResultMap.put("skuInfo", skuInfo);
            }
            return skuInfo;
        }, executor);

        //根据商品Sku三家分类ID查询分类信息
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            if (categoryView != null) {
                ResultMap.put("categoryView", categoryView);
            }
        }, executor);

        //根据SKuID查询价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            if (skuPrice != null) {
                ResultMap.put("price", skuPrice);
            }
        }, executor);

        //根据Sku所属的SpuID查询海报图片列表
        CompletableFuture<Void> spuPosterListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            if (!CollectionUtils.isEmpty(spuPosterList)) {
                ResultMap.put("spuPosterList", spuPosterList);
            }
        }, executor);

        //根据SkuID查询商品平台属性列表
        CompletableFuture<Void> skuAttrListCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> skuAttrList = productFeignClient.getAttrList(skuId);
            if (!CollectionUtils.isEmpty(skuAttrList)) {
                ResultMap.put("skuAttrList", skuAttrList);
            }
        }, executor);

        //根据spuId,skuId查询当前商品销售属性(带选中效果)
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
                ResultMap.put("spuSaleAttrList", spuSaleAttrList);
            }
        }, executor);

        //切换SKU转换SKU商品json字符串信息
        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            if (!CollectionUtils.isEmpty(skuValueIdsMap)) {
                ResultMap.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
            }
        }, executor);

        //远程调用搜索微服务：更新商品得分分值
        CompletableFuture<Void> incrScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        });

        //组合多个异步任务对象，必须等待所有任务执行完毕
        CompletableFuture.allOf(
                skuAttrListCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuPosterListCompletableFuture,
                skuAttrListCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture,
                incrScoreCompletableFuture
        ).join();
        return ResultMap;
    }
}
